/***
This do-file retrieves some numbers we use in the paper.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Spending"

* Create required subfolders
cap mkdir "${root}/results/Spending"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

*-------------------------------------------------------------------------------
* Prepare data
*-------------------------------------------------------------------------------

* Load posted Affinity data
project, uses("${root}/data/web/data/Affinity - National - Daily.csv")
import delimited using "${root}/data/web/data/Affinity - National - Daily.csv", clear

keep year month day spend_all spend_all_q1 spend_all_q2 spend_all_q3 spend_all_q4 
order year month day spend_all spend_all_q1 spend_all_q2 spend_all_q3 spend_all_q4 

gen date = mdy(month, day, year)
format date %td
order date

keep if inrange(date, mdy(1, 1, 2020), $finaldate)

tw  (line spend_all_q1 date) ///
	(line spend_all_q2 date) ///
	(line spend_all_q3 date) ///
	(line spend_all_q4 date), ///
	legend(order(1 "Q1" 2 "Q2" 3 "Q3" 4 "Q4")) ///
	ylabel(,nogrid)

* Spending changes from Jan 2020 to mid-April 2020
sum spend_all if date == mdy(4, 15, 2020)
local spending_midapril_all = round(`=`r(mean)'*100', 1)

sum spend_all_q1 if date == mdy(4, 15, 2020)
local spending_midapril_q1 = round(`=`r(mean)'*100', 1)

sum spend_all_q2 if date == mdy(4, 15, 2020)
local spending_midapril_q2 =round(`=`r(mean)'*100', 1)

sum spend_all_q3 if date == mdy(4, 15, 2020)
local spending_midapril_q3 = round(`=`r(mean)'*100', 1)

sum spend_all_q4 if date == mdy(4, 15, 2020)
local spending_midapril_q4 = round(`=`r(mean)'*100', 1)

* Spending changes from Jan 2020 to December 2021
sum spend_all if date == mdy(12, 31, 2021)
local spending_dec2021_all = round(`=`r(mean)'*100', 1)

sum spend_all_q1 if date == mdy(12, 31, 2021)
local spending_dec2021_q1 = round(`=`r(mean)'*100', 1)

sum spend_all_q2 if date == mdy(12, 31, 2021)
local spending_dec2021_q2 =round(`=`r(mean)'*100', 1)

sum spend_all_q3 if date == mdy(12, 31, 2021)
local spending_dec2021_q3 = round(`=`r(mean)'*100', 1)

sum spend_all_q4 if date == mdy(12, 31, 2021)
local spending_dec2021_q4 = round(`=`r(mean)'*100', 1)

*-------------------------------------------------------------------------------
* Export output numbers to csv file
*-------------------------------------------------------------------------------

cap erase "${root}/results/paper numbers/`category'/Spending Series by Income Quartile.yaml"

yamlout using "${root}/results/paper numbers/`category'/Spending Series by Income Quartile.yaml", ///
		key("spending_midapril_all") ///
		comment("Spending series all in April 15 2020") ///
		value("`=abs(`spending_midapril_all')'")
yamlout using "${root}/results/paper numbers/`category'/Spending Series by Income Quartile.yaml", ///
		key("spending_dec2021_all") ///
		comment("Spending series all in December 31 2020") ///
		value("`=abs(`spending_dec2021_all')'")
		
foreach q in 1 4  {
	yamlout using "${root}/results/paper numbers/`category'/Spending Series by Income Quartile.yaml", ///
		key("spending_midapril_q`q'") ///
		comment("Spending series Q`q' in April 15 2020") ///
		value("`=abs(`spending_midapril_q`q'')'")
		
	yamlout using "${root}/results/paper numbers/`category'/Spending Series by Income Quartile.yaml", ///
		key("spending_dec2021_q`q'") ///
		comment("Spending series Q`q' in December 31 2021") ///
		value("`=abs(`spending_dec2021_q`q'')'")
}

project, creates("${root}/results/paper numbers/`category'/Spending Series by Income Quartile.yaml")
